package com.dx.mes.service.query.service;


import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import com.dx.common.table.ppt.*;
import com.dx.mes.common.dao.*;

import com.dx.mes.facade.query.tx.Fipinqbox.*;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.dx.mes.common.table.rtnBean.NotFoundRtnBeen;
import com.dx.mes.common.transcoder.util.AppContext;
import com.dx.mes.common.transcoder.util.HCutil;
import com.dx.mes.common.transcoder.util.JacksonUtil;
import com.dx.mes.common.transcoder.util.LogUtils;
import com.dx.mes.common.transcoder.util.Util;
import com.dx.mes.facade.query.service.IFipinqboxService;

import static com.dx.mes.common.bean.def.GenericDef.*;


@Service("fipinqboxService")
public class FipinqboxService implements IFipinqboxService{
	private LogUtils logUtils;
	private static final String TX_NAME = "FIPINQBOX";
	
	@Autowired
	private IRetBoxDao retBoxDao;
	@Autowired
	private IRetPrdInfoDao retPrdInfoDao;
	@Autowired
	private IRetWODao retWoDao;
	@Autowired
	private IBisDataDao bisDataDao;
	@Autowired
	private IRetBoxXfrDao retBoxXfrDao;
	@Autowired
	private IRetToolPtDao retToolPtDao;
	@Autowired
	private IBisMdlDefDao bisMdlDefDao;
	@Autowired
	private IBisCusDao  bisCusDao;
	@Autowired
	private IRetPrdInDao retPrdInDao;
	@Transactional
	public String subMainProc(String evt_no,String strInMsg) {
		AppContext.clear();
		AppContext.setCurrServiceName(FipinqboxService.class.getSimpleName());
		AppContext.setCurrEventNumber(evt_no);
		logUtils = new LogUtils(FipinqboxService.class);
		logUtils.info("[InTrx:" + strInMsg + "]");
		Session session = retBoxDao.getCrSession();
		String outMsg = "";
		
		FipinqboxI inTrx = new FipinqboxI();
		FipinqboxO outTrx = new FipinqboxO();

		outTrx.setTrx_id(TX_NAME);
		outTrx.setType_id(TRX_OUT);
		outTrx.setRtn_code(String.valueOf(RETURN_CODE_SET_ERROR));

		try {
			inTrx = JacksonUtil.fromJson(strInMsg, FipinqboxI.class);

			subMainProc2(inTrx.getAction_flg().toCharArray()[0],inTrx,outTrx);
			if (RETURN_CODE_OK.equals(outTrx.getRtn_code())) {
				session.flush();
			} else {
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			outTrx.setRtn_mesg(ex.toString());
			outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
			logUtils.error(Util.stackTraceToString(ex));
			TransactionAspectSupport.currentTransactionStatus()
					.setRollbackOnly();
		} finally {
			outMsg = JacksonUtil.toJSONStr(outTrx);
			logUtils.info("[OutTrx:" + outMsg + "]");
		}
		return outMsg;
	}

	private void subMainProc2(char actionType,FipinqboxI inTrx,FipinqboxO outTrx) {
	
		switch (actionType) {
			case 'C': {
				getBoxtableInfoByCondition(inTrx,outTrx);
				break;
			}
			case 'Q': {
				getBoxInfo(inTrx,outTrx);
				break;
			}
			case 'T': {
				 getBoxtableInfo(inTrx,outTrx);
				break;
			}
			case 'W': {
				getBoxInfoByWO(inTrx,outTrx);
				break;
			}
			case 'D': {
				getBoxTableByTool(inTrx,outTrx);
				break;
			}
			case 'B': {
				queryBoxListByLogOn(inTrx,outTrx);
				break;
			}
			case 'I' :{
				getBoxAndPrdCrInfo(inTrx,outTrx);
				break;
			}
			case 'P' :{
				getBoxByPallect(inTrx,outTrx);
				break;
			}
			case 'V' :{
				findBoxByQuery(inTrx,outTrx);
				break;
			}
			case 'M' :{
				getBoxPack(inTrx,outTrx);
				break;
			}
			case 'N' :{
				queryForPrint(inTrx,outTrx);
				break;
			}
			case 'E' :{
				queryForMtrlBox(inTrx,outTrx);
				break;
			}
			default: {
				String gszMsgBuf = "Invalide type input: [" + actionType + "]";
				outTrx.setRtn_mesg(gszMsgBuf);
				outTrx.setRtn_code(String.valueOf(E_FIPINQBOX_INVALID_INPUT));
			}
		}
	}

	

	private void findBoxByQuery(FipinqboxI inTrx, FipinqboxO outTrx) {
		String hql = "FROM Ret_box WHERE 1=1 ";
		int i=0;
		for(i = 0;i<inTrx.getIary().size();i++){
			FipinqboxIA iary = inTrx.getIary().get(i);
			String condition = iary.getQuery_codition();
			String value = iary.getQuery_value();
			if (!Util.isSpaceCheck(condition)) {
				hql +="and "+condition+" in("+value+")";
			}
		}
		for(i = 0;i<inTrx.getIary().size();i++){
			FipinqboxIA iary = inTrx.getIary().get(i);
			String condition = iary.getQuery_codition();
			if(condition.equals("WO_ID_FK")){
				hql +=" order by wo_id_fk";
			}
		}

		List<Ret_box> boxList = retBoxDao.find(hql);
		List<FipinqboxO> fipinqboxOS = new ArrayList<FipinqboxO>();

		outTrx.setBox_cnt(0);
		if (null == boxList || boxList.size() == 0) {
			outTrx.setBox_cnt(0);
		} else {
			outTrx.setBox_cnt(boxList.size());
			outTrx.setOary_box(boxList);
		}
//		for(Ret_box ret_box : boxList){
//			FipinqboxO oary = new FipinqboxO();
//			oary.setBox_id(ret_box.getBox_id());
//			oary.setBox_stat(ret_box.getBox_stat());
//		}

		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
	}

	/**
	 * 根据设备号查询箱子信息，返回整表数据
	 * 
	 * @return
	 */
	private void getBoxTableByTool(FipinqboxI inTrx, FipinqboxO outTrx) {
		// TODO Auto-generated method stub
		String sql = "FROM Ret_box WHERE 1=1 ";
		if (!Util.isSpaceCheck(inTrx.getTool_id())) {
			sql += " AND tool_id_fk = '" + inTrx.getTool_id() + "' ";
		}
		List<Ret_box> boxList = new ArrayList<Ret_box>();
		boxList = retBoxDao.find(sql);
		outTrx.setBox_cnt(0);
		if (null == boxList) {
			outTrx.setBox_cnt(0);
		} else {
			outTrx.setBox_cnt(boxList.size());
			outTrx.setOary_box(boxList);
		}
		
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
	}

	/**
	 * 根据箱号查询箱子信息，返回整表数据
	 * 
	 * @return
	 */
	private void getBoxtableInfo(FipinqboxI inTrx,FipinqboxO outTrx) {
		
		
		if (Util.isSpaceCheck(inTrx.getBox_id())) {
			String gszMsgBuf = "Input box id [" + inTrx.getBox_id() + "] is null!";
			outTrx.setRtn_mesg(gszMsgBuf);
            outTrx.setRtn_code(String.valueOf(E_FIPINQBOX_INVALID_INPUT_BOX_ID));		
            return;
		}

		Ret_box box = new Ret_box();
		box = retBoxDao.get(inTrx.getBox_id());
		if (null == box) {
			String gszMsgBuf = new NotFoundRtnBeen(inTrx.getBox_id(), Ret_box.class)
					.toString();
			outTrx.setRtn_mesg(gszMsgBuf);
			outTrx.setRtn_code(String.valueOf( E_RET_BOX + E_READ_NOT_FOUND));
			return;
			
		}

		outTrx.setRet_box(box);
		outTrx.setBox_cnt(1);
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);

	}

	
	private void queryBoxListByLogOn(FipinqboxI inTrx, FipinqboxO outTrx) {
		List<Ret_box> boxList = new ArrayList<Ret_box>();
		Ret_tool_ptId ret_tool_ptId = new Ret_tool_ptId(); 
		String tool_port_id_fk = inTrx.getTool_port_id_fk();
		String tool_id_fk = inTrx.getTool_id();
		ret_tool_ptId.setPort_id(tool_port_id_fk);
		ret_tool_ptId.setTool_id_fk(tool_id_fk);
		Ret_tool_pt ret_tool_pt = retToolPtDao.get(ret_tool_ptId);
		if (null == ret_tool_pt) {
			String gszMsgBuf = "Input tool port id [" + ret_tool_pt.getPort_typ() + "] is not exist!";
			outTrx.setRtn_mesg(gszMsgBuf);
            outTrx.setRtn_code(String.valueOf(E_FIPINQBOX_INVALID_INPUT_TOOL_PORT_ID));		
            return;
		}
		
		if (!ret_tool_pt.getPort_typ().equals("L")) {
			String gszMsgBuf = "Input tool port id [" + ret_tool_pt.getPort_typ() + "] is not 'L'!";
			outTrx.setRtn_mesg(gszMsgBuf);
            outTrx.setRtn_code(String.valueOf(E_FIPINQBOX_INVALID_INPUT_TOOL_PORT_ID));		
            return;
		}
		String hql = "FROM Ret_box where nx_ope_id_fk = '"+inTrx.getNx_ope_id_fk()+"' and box_stat = 'WAIT'";
		List<Ret_box> retBoxList = retBoxDao.find(hql);
		if (!retBoxList.isEmpty()) {
			boxList.addAll(retBoxList);
		}
		String hql2 = "FROM Ret_box where tool_id_fk = '"+inTrx.getTool_id()+"' and tool_port_id_fk = '"+
		inTrx.getTool_port_id_fk()+"' and box_stat= 'INPR'";
		List<Ret_box> inprBoxList = retBoxDao.find(hql2);
		if (!inprBoxList.isEmpty()) {
			boxList.addAll(inprBoxList);
		}
		
		outTrx.setBox_cnt(boxList.size());
		outTrx.setOary_box(boxList);
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
	}
	
	/**
	 * 查询箱子信息，返回符合条件的箱子表
	 * 
	 * @return
	 */
	private void getBoxtableInfoByCondition(FipinqboxI inTrx,FipinqboxO outTrx) {

		String sql = "FROM Ret_box WHERE 1=1 ";

		if (!Util.isSpaceCheck(inTrx.getBox_id())) {
			sql += " AND box_id='" + inTrx.getBox_id() + "'";
		}
		if (!Util.isSpaceCheck(inTrx.getBox_stat())) {
			sql += " AND box_stat='" + inTrx.getBox_stat() + "'";
		}
		if (!Util.isSpaceCheck(inTrx.getBnk_flg())) {
			sql += " AND bnk_flg = '" + inTrx.getBnk_flg() + "' ";
		}
		if (!Util.isSpaceCheck(inTrx.getWo_id())) {
			sql += " AND wo_id_fk = '" + inTrx.getWo_id() + "' ";
		}
		if (!Util.isSpaceCheck(inTrx.getTool_id())) {
			sql += " AND tool_id_fk = '" + inTrx.getTool_id() + "' ";
//			sql += " AND prd_qty > 1 ";
//			sql += " AND box_stat in('WAIT','INPR')";
		}
		if(!Util.isSpaceCheck(inTrx.getDest_shop())){
			sql += " AND dest_shop ='" + inTrx.getDest_shop() +"' ";
		}
		if(!Util.isSpaceCheck(inTrx.getEvt_cate())){
			sql += " AND evt_cate ='" + inTrx.getEvt_cate() +"' ";
		}
		if (!Util.isSpaceCheck(inTrx.getLot_id())) {
			sql += "AND lot_id_fk like ('%" + inTrx.getLot_id() + "%') ";
		}
		if (!Util.isSpaceCheck(inTrx.getNx_ope_id_fk())) {
			sql += "AND nx_ope_id_fk = '" + inTrx.getNx_ope_id_fk() + "'";
		}
		if (!Util.isSpaceCheck(inTrx.getTool_port_id_fk())) {
			sql += "AND tool_port_id_fk = '" + inTrx.getTool_port_id_fk() + "'";
		}
		List<Ret_box> boxList = new ArrayList<Ret_box>();
		boxList = retBoxDao.find(sql);
		outTrx.setBox_cnt(0);
		if (null == boxList) {
			outTrx.setBox_cnt(0);
		} else {
			outTrx.setBox_cnt(boxList.size());
			outTrx.setOary_box(boxList);
		}
		outTrx.setTbl_cnt(boxList.size());
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);

	}
	
	/**
	 * Query boxes info by wo_id
	 * 
	 * @return
	 */
	private void getBoxInfoByWO(FipinqboxI inTrx,FipinqboxO outTrx) {
		
		String queryBoxStat = PROC_WAIT;

		if (Util.isSpaceCheck(inTrx.getWo_id())) {
			outTrx.setRtn_mesg("输入的工单代码为空!");
			outTrx.setRtn_code(String.valueOf(E_FIPINQBOX_INVALID_INPUT_WO_ID));
			return;
		}
		if (!Util.isSpaceCheck(inTrx.getBox_stat())) {
			queryBoxStat = inTrx.getBox_stat();
		}

		String sql = "FROM Ret_box WHERE BOX_STAT='" + queryBoxStat
				+ "' AND WO_ID_FK='" + inTrx.getWo_id() + "' ";
		if (!Util.isSpaceCheck(inTrx.getDest_shop())) {
			sql += " AND dest_shop = '" + inTrx.getDest_shop() + "' ";
		}
		List<Ret_box> boxList = new ArrayList<Ret_box>();
		boxList = retBoxDao.find(sql);
		outTrx.setBox_cnt(0);
		if (null == boxList) {
			outTrx.setBox_cnt(0);
		} else {
			outTrx.setBox_cnt(boxList.size());
			outTrx.setOary_box(boxList);
		}

		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
	}

	/**
	 * Only query box info
	 * 
	 * @return
	 */


	private void getBoxPack(FipinqboxI inTrx,FipinqboxO outTrx) {

		Ret_box box = new Ret_box();
		box = retBoxDao.get(inTrx.getBox_id());
		if(box==null){
			outTrx.setRtn_mesg("箱子["+inTrx.getBox_id()+"]信息不存在！");
			outTrx.setRtn_code(String.valueOf(E_RET_BOX + E_READ_NOT_FOUND));
			return;
		}
		String boxStat = box.getBox_stat();
		if(!"COMP".equals(boxStat)){
			outTrx.setRtn_mesg("该箱状态为["+boxStat+"],不能进行外包装！");
			outTrx.setRtn_code(String.valueOf(E_RET_BOX + E_READ_NOT_FOUND));
			return;
		}
		String shipBoxId = box.getShip_box_id();
		if(!StringUtils.isEmpty(shipBoxId)){
			outTrx.setRtn_mesg("该箱已经被外包了，请重新选择！");
			outTrx.setRtn_code(String.valueOf(E_RET_BOX + E_READ_NOT_FOUND));
			return;
		}
		if (null == box) {
			outTrx.setRtn_mesg(new NotFoundRtnBeen(inTrx.getBox_id(), Ret_box.class).toString());
			outTrx.setRtn_code(String.valueOf(E_RET_BOX + E_READ_NOT_FOUND));
			return;
		}

		outTrx.setBox_id(box.getBox_id());
		outTrx.setMdl_id_fk(box.getMdl_id_fk());
		outTrx.setPrd_grade_fk(box.getPrd_grade_fk());
		outTrx.setPrd_qty(box.getPrd_qty());
		outTrx.setWo_id_fk(box.getWo_id_fk());
		outTrx.setBox_cate(box.getBox_cate());

		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
	}
	private void getBoxInfo(FipinqboxI inTrx,FipinqboxO outTrx) {

		if (Util.isSpaceCheck(inTrx.getBox_id())) {
			String gszMsgBuf = "Input box id [" + inTrx.getBox_id() + "] is null!";

			outTrx.setRtn_mesg(gszMsgBuf);
			outTrx.setRtn_code(String.valueOf(E_FIPINQBOX_INVALID_INPUT_BOX_ID));
		}

		Ret_box box = new Ret_box();
		box = retBoxDao.get(inTrx.getBox_id());
		if (null == box) {
			outTrx.setRtn_mesg(new NotFoundRtnBeen(inTrx.getBox_id(), Ret_box.class).toString());
			outTrx.setRtn_code(String.valueOf(E_RET_BOX + E_READ_NOT_FOUND));
			return;
		}
		//获取 box for prd max count
		String hql = "From Bis_data where data_cate='BXSC' AND data_ext='" + box.getBox_set_code() + "'";
		List<Bis_data> bisDataList = bisDataDao.find(hql);
		if (bisDataList.size() > 0) {
			outTrx.setMax_cnt(bisDataList.get(0).getExt_1());
			outTrx.setMax_use_cnt(bisDataList.get(0).getExt_2());
		}

		seOutTrxByRetBox(box, inTrx, outTrx);
		if (!Util.isSpaceCheck(box.getWo_id_fk())) {
			Ret_wo ret_wo = new Ret_wo();
			ret_wo = retWoDao.get(box.getWo_id_fk());
			if (ret_wo != null) {
				outTrx.setCus_id_fk(ret_wo.getCus_id_fk());
				outTrx.setFrom_thickness(ret_wo.getFrom_thickness().toString());
				outTrx.setCus_mtrl_id_fk(ret_wo.getCus_mtrl_id_fk());
				outTrx.setWo_cate(ret_wo.getWo_cate());
				outTrx.setWo_dsc(ret_wo.getWo_dsc());
				outTrx.setPln_cmp_timestamp(ret_wo.getPln_cmp_timestamp().toString());
			} else {
				outTrx.setRtn_mesg(new NotFoundRtnBeen(box.getWo_id_fk(), Ret_wo.class).toString());
				outTrx.setRtn_code(String.valueOf(E_RET_WO + E_READ_NOT_FOUND));
				return;
			}
		}

		//Ret_box_xfr ret_box_xfr = retBoxXfrDao.get(inTrx.getBox_id());

		List<FipinqboxOB> fipinqboxOBs = new ArrayList<FipinqboxOB>(); 
		/*if (ret_box_xfr != null ) {
			setOutTrxByXfr(ret_box_xfr,inTrx,outTrx,fipinqboxOBs);
		}*/
		if (!PROC_EMPTY.equals(box.getBox_stat())) {
			List<Ret_prd_info> ret_prd_infoList = retPrdInfoDao.getShtListInfo(inTrx.getBox_id());
			if (ret_prd_infoList != null) {
				if (!ret_prd_infoList.isEmpty()) {
					outTrx.setOary(ret_prd_infoList);
				}
			} else{
			outTrx.setRtn_mesg("该卡匣中没有玻璃");
			outTrx.setRtn_code(String.valueOf(E_RET_PRD_INFO + E_READ_NOT_FOUND));
			return;
		}
			if (StringUtils.isNoneEmpty(box.getMdl_id_fk())) {
				//查询客户名称
				Bis_mdl_def bis_mdl_def = bisMdlDefDao.get(box.getMdl_id_fk());
				if (bis_mdl_def != null) {
					outTrx.setCus_mdl_id(bis_mdl_def.getCus_mdl_id());
					Bis_cus_loc bis_cus_loc = bisCusDao.get(bis_mdl_def.getCus_id());
					if (bis_cus_loc != null) {
						outTrx.setCus_name(bis_cus_loc.getCus_name());
					} else {
						outTrx.setRtn_mesg("客户代码[" + box.getMdl_id_fk() + "]不存在，请确认！");
						outTrx.setRtn_code(String.valueOf(E_BIS_CUS_LOC + E_READ_NOT_FOUND));
						return;
					}
				} else {
					outTrx.setRtn_mesg("产品代码[" + box.getMdl_id_fk() + "]不存在，请确认！");
					outTrx.setRtn_code(String.valueOf(E_BIS_MDL_DEF + E_READ_NOT_FOUND));
					return;
				}

			}
		outTrx.setOaryB(fipinqboxOBs);
		}
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
	}
//	/**
//	 * Query prd list in box and set to oary
//	 */
//	private void setOutTrxByPrdList(String boxID,FipinqboxI inTrx,FipinqboxO outTrx) {
//		
//		List<Ret_prd_info> ret_prd_infoList = new ArrayList<Ret_prd_info>();
//		if (_YES.equals(inTrx.getOrder_slot_flg())) {
//			ret_prd_infoList = retPrdInfoDao.getShtListInfoOrderBySlotDesc(boxID);
//		} else {
//			if (inTrx.getWo_id() != null && !Util.isSpaceCheck(inTrx.getWo_id()) ) {
//				ret_prd_infoList = retPrdInfoDao.getShtListInfoWithWo(boxID, inTrx.getWo_id());
//			}else {
//				ret_prd_infoList = retPrdInfoDao.getShtListInfo(boxID);
//			}
//		}
//		if (ret_prd_infoList != null ){
//			if (!ret_prd_infoList.isEmpty()) {
//				outTrx.setOary(ret_prd_infoList);
//				
//			}
//		}
//		if (!Util.isSpaceCheck(inTrx.getFirst_ope_id())) {
//			for (Ret_prd_info prd : ret_prd_infoList) {
//				String path_id = prd.getCr_path_id_fk();
//				String path_ver = prd.getCr_path_ver_fk();
//				
//				if (Util.isSpaceCheck(path_id) || Util.isSpaceCheck(path_ver)) {
//					String gszMsgBuf = "该面板["+prd.getPrd_seq_id()+"]工艺线路信息不存在";
//					
//					outTrx.setRtn_code(String.valueOf(E_XPINQBOX_INVALID_INPUT_BOX_ID));
//					outTrx.setRtn_mesg(gszMsgBuf);
//					
//					return;
//				}
//				
//				Bis_pathiId bis_pathiId = new Bis_pathiId();
//				bis_pathiId.setPath_id(path_id);
//				bis_pathiId.setPath_ver(path_ver);
//				
//				Bis_pathi bis_pathi = new Bis_pathi();
//				bis_pathi = bisPathiDao.get(bis_pathiId);
//				if (null == bis_pathi) {
//					String gszMsgBuf = "该工艺线路不存在"+path_id;
//
//                    outTrx.setRtn_code(String.valueOf(E_XPINQBOX_INVALID_INPUT_BOX_ID));
//                    outTrx.setRtn_mesg(gszMsgBuf);
//                    return;
//				}
//				outTrx.setFirst_ope_id(bis_pathi.getFirst_ope_no());
//				break;
//			}
//		}
//
//		outTrx.setRtn_code(RETURN_CODE_OK);
//		outTrx.setRtn_mesg(RETURN_MESG_OK);
//	}

	private void setOutTrxByXfr(Ret_box_xfr ret_box_xfr, FipinqboxI inTrx, FipinqboxO outTrx,List<FipinqboxOB> fipinqboxOBs) {
		FipinqboxOB fipinqboxOB = new FipinqboxOB();
		
		fipinqboxOB.setXfr_pos_id(ret_box_xfr.getXfr_pos_id());      
		fipinqboxOB.setXfr_port_id(ret_box_xfr.getXfr_port_id());
		fipinqboxOB.setXfr_pati_id(ret_box_xfr.getXfr_pati_id());                       
		fipinqboxOB.setXfr_zone_id(ret_box_xfr.getXfr_zone_id());                      
		fipinqboxOB.setXfr_shelf_id(ret_box_xfr.getXfr_shelf_id());
		String evtdateStr = "";
		String lstStkInStr = "";
		DateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		if (null != ret_box_xfr.getEvt_timestamp()) {
			evtdateStr = sdf.format(ret_box_xfr.getEvt_timestamp());
			fipinqboxOB.setEvt_timestamp(ret_box_xfr.getEvt_timestamp().toString());             
		}
		fipinqboxOB.setEvt_date(evtdateStr);
		fipinqboxOB.setXfr_stat(ret_box_xfr.getXfr_stat());                     
		fipinqboxOB.setXfr_cmd_flg(ret_box_xfr.getXfr_cmd_flg());                          
		fipinqboxOB.setXfr_fr_tool_id(ret_box_xfr.getXfr_fr_tool_id());                      
		fipinqboxOB.setXfr_fr_port_id(ret_box_xfr.getXfr_fr_port_id());                    
		fipinqboxOB.setXfr_fr_pati_id(ret_box_xfr.getXfr_fr_pati_id());                    
		fipinqboxOB.setXfr_fr_zone_id(ret_box_xfr.getXfr_fr_zone_id());                    
		fipinqboxOB.setXfr_to_tool_id(ret_box_xfr.getXfr_to_tool_id());                  
		fipinqboxOB.setXfr_to_port_id(ret_box_xfr.getXfr_to_port_id());                    
		fipinqboxOB.setXfr_to_pati_id(ret_box_xfr.getXfr_to_pati_id());                    
		fipinqboxOB.setXfr_to_zone_id(ret_box_xfr.getXfr_to_zone_id());       
		if (null != ret_box_xfr.getXfr_lst_stk_in_timestamp()) {
			lstStkInStr = sdf.format(ret_box_xfr.getXfr_lst_stk_in_timestamp());
			fipinqboxOB.setXfr_lst_stk_in_timestamp(ret_box_xfr.getXfr_lst_stk_in_timestamp().toString());
		}
		fipinqboxOB.setXfr_lst_stk_in_date(lstStkInStr);

		     
		fipinqboxOBs.add(fipinqboxOB);
		                                       
		outTrx.setRtn_code(RETURN_CODE_OK);                                
		outTrx.setRtn_mesg(RETURN_MESG_OK);
	}

	/**
	 * Set outTrx info by ret_box object
	 * 
	 * @param box
	 */
	private void seOutTrxByRetBox(Ret_box box,FipinqboxI inTrx,FipinqboxO outTrx) {
		
		outTrx.setBox_id(box.getBox_id());
		outTrx.setBox_stat(box.getBox_stat());
		outTrx.setValid_flg(box.getValid_flg());
		outTrx.setGroup_id(box.getGroup_id());
		outTrx.setPpbox_id(box.getPpbox_id());
		outTrx.setReal_emp(box.getReal_emp());
		outTrx.setBox_use_cnt(box.getBox_use_cnt());
		outTrx.setPrd_grade_fk(box.getPrd_grade_fk());
		outTrx.setPrd_qty(box.getPrd_qty());
		outTrx.setMdl_id_fk(box.getMdl_id_fk());
		outTrx.setMtrl_prod_id_fk(box.getMtrl_prod_id_fk());
		outTrx.setNx_path_id_fk(box.getNx_path_id_fk());
		outTrx.setNx_path_ver_fk(box.getNx_path_ver_fk());
		outTrx.setNx_ope_no_fk(box.getNx_ope_no_fk());
		outTrx.setNx_ope_id_fk(box.getNx_ope_id_fk());
		outTrx.setNx_ope_ver_fk(box.getNx_ope_ver_fk());
		outTrx.setNx_proc_id_fk(box.getNx_proc_id_fk());
		outTrx.setNx_toolg_id_fk(box.getNx_toolg_id_fk());
		outTrx.setNx_tool_id_fk(box.getNx_tool_id_fk());
		outTrx.setTool_id_fk(box.getTool_id_fk());
		outTrx.setTool_port_id_fk(box.getTool_port_id_fk());
		if(box.getAct_cmp_timestamp() != null){
			outTrx.setAct_cmp_timestamp(box.getAct_cmp_timestamp().toString());
		}
		if( box.getWh_in_timestamp() != null){
			outTrx.setWh_in_timestamp(box.getWh_in_timestamp().toString());
		}
		outTrx.setPrty(box.getPrty());
		outTrx.setPv_prty(box.getPv_prty());
		outTrx.setOqc_lotid(box.getOqc_lotid());
		outTrx.setOqc_skip_flg(box.getOqc_skip_flg());
		outTrx.setWo_id_fk(box.getWo_id_fk());
		outTrx.setDest_shop(box.getDest_shop());
		outTrx.setSo_id_fk(box.getSo_id_fk());
		outTrx.setPallet_id_fk(box.getPallet_id_fk());
		outTrx.setBatch_no_fk(box.getBatch_no_fk());
		outTrx.setRsv_no_fk(box.getRsv_no_fk());
		outTrx.setDn_no_fk(box.getDn_no_fk());
		outTrx.setCus_no(box.getCus_no());
		outTrx.setSn_no(box.getSn_no());
		outTrx.setShip_box_id(box.getShip_box_id());
		if( box.getShipping_timestamp() != null){
			outTrx.setShipping_timestamp(box.getShipping_timestamp().toString());
		}
		if(box.getAct_cmp_timestamp() != null){
			outTrx.setAct_cmp_timestamp(box.getAct_cmp_timestamp().toString());
		}
		if( box.getEvt_timestamp() != null){
			outTrx.setEvt_timestamp(box.getEvt_timestamp().toString());
		}

		outTrx.setBox_cate(box.getBox_cate());
		outTrx.setBox_set_code(box.getBox_set_code());
//		outTrx.setBox_rgst_timestamp(box.getBox_rgst_timestamp().toString());
		outTrx.setBox_own(box.getBox_own());
		outTrx.setBnk_flg(box.getBnk_flg());
		outTrx.setEvt_usr(box.getEvt_usr());
		outTrx.setBox_cnt(1);
		outTrx.setChip_qty(box.getChip_qty());
		outTrx.setBox_clean_flg(box.getBox_clean_flg());
		outTrx.setBox_note(box.getBox_note());
		
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
	}

	/**
	 * 根据箱号查询箱子信息和玻璃信息，分WAIT和INPR
	 * @param inTrx
	 * @param outTrx
	 */
	private void getBoxAndPrdCrInfo(FipinqboxI inTrx,FipinqboxO outTrx){
		List<FipinqboxOC> oList = new ArrayList<FipinqboxOC>();
		if (Util.isSpaceCheck(inTrx.getBox_id())) {
			String gszMsgBuf = "Input box id [" + inTrx.getBox_id() + "] is null!";
			outTrx.setRtn_mesg(gszMsgBuf);
			outTrx.setRtn_code(String.valueOf(E_FIPINQBOX_INVALID_INPUT_BOX_ID));
			return;
		}

		Ret_box box = retBoxDao.get(inTrx.getBox_id());
        if (null == box) {
            outTrx.setRtn_mesg(new NotFoundRtnBeen(inTrx.getBox_id(), Ret_box.class).toString());
            outTrx.setRtn_code(String.valueOf(E_RET_BOX + E_READ_NOT_FOUND));
            return;
        }
		seOutTrxByRetBox(box,inTrx,outTrx);
		if(!box.getBox_stat().equals(PROC_EMPTY)){
			Bis_mdl_def bis_mdl_def = bisMdlDefDao.get(box.getMdl_id_fk());
			if(bis_mdl_def != null){
                outTrx.setCus_id_fk(bis_mdl_def.getCus_id());
            }else{
                outTrx.setRtn_mesg("产品代码["+box.getMdl_id_fk()+"]不存在，请确认！");
                outTrx.setRtn_code(String.valueOf(E_BIS_MDL_DEF + E_READ_NOT_FOUND));
                return;
            }
		}
		String hql = "From Bis_data where data_cate='BXSC' AND data_ext='" + box.getBox_set_code() + "'";
		List<Bis_data> bisDataList = bisDataDao.find(hql);
		if (bisDataList.size() > 0) {
			outTrx.setMax_cnt(bisDataList.get(0).getExt_1());
			outTrx.setMax_use_cnt(bisDataList.get(0).getExt_2());
		}
		List<Ret_prd_info> ret_prd_infos = retPrdInfoDao.getShtListInfo(inTrx.getBox_id());
		if(ret_prd_infos != null && ret_prd_infos.size() >0){
			
			for(Ret_prd_info ret_prd_info : ret_prd_infos){
				FipinqboxOC oaryC = new FipinqboxOC();
				oaryC.setPrd_seq_id(ret_prd_info.getPrd_seq_id());
				oaryC.setPrd_qty(ret_prd_info.getPrd_qty().toString());
				oaryC.setPrd_stat(ret_prd_info.getPrd_stat());
				oaryC.setTemp_prd_seq_id(ret_prd_info.getTemp_prd_seq_id());
				oaryC.setMdl_id_fk(ret_prd_info.getMdl_id_fk());
				oaryC.setPrd_grade(ret_prd_info.getPrd_grade());
				oaryC.setSlot_no(ret_prd_info.getSlot_no());
				oaryC.setWo_id_fk(ret_prd_info.getWo_id_fk());
				oaryC.setBox_id_fk(ret_prd_info.getBox_id_fk());
				oaryC.setAbormal_flg(ret_prd_info.getAbnormal_flg());
				if(ret_prd_info.getPrd_stat().equals(PROC_IN_PROCESS)){
					oaryC.setOpe_id_fk(ret_prd_info.getCr_ope_id_fk());
					oaryC.setOpe_ver_fk(ret_prd_info.getCr_ope_ver_fk());
					oaryC.setPath_id_fk(ret_prd_info.getCr_path_id_fk());
					oaryC.setPath_ver_fk(ret_prd_info.getCr_path_ver_fk());
					oaryC.setTool_id_fk(ret_prd_info.getCr_tool_id_fk());
					oaryC.setTool_port_id_fk(ret_prd_info.getTool_port_id_fk());
					oaryC.setTool_run_mode(ret_prd_info.getCr_tool_run_mode());
					oaryC.setProc_id_fk(ret_prd_info.getCr_proc_id_fk());
				}else if(ret_prd_info.getPrd_stat().equals(PROC_WAIT)){
					oaryC.setOpe_id_fk(ret_prd_info.getNx_ope_id_fk());
					oaryC.setOpe_ver_fk(ret_prd_info.getNx_ope_ver_fk());
					oaryC.setPath_id_fk(ret_prd_info.getNx_path_id_fk());
					oaryC.setPath_ver_fk(ret_prd_info.getNx_path_ver_fk());
					oaryC.setTool_id_fk(ret_prd_info.getNx_tool_id_fk());
					oaryC.setTool_run_mode(ret_prd_info.getNx_tool_run_mode());
					oaryC.setProc_id_fk(ret_prd_info.getNx_proc_id_fk());
				}
				oList.add(oaryC);
			}
		}
		List <FipinqboxOD>  dList = new ArrayList<>();
		String hqls ="From Ret_box where ship_box_id='" + inTrx.getBox_id() + "'";
		List<Ret_box> boxList =  retBoxDao.find(hqls);
		for(Ret_box ret_box :boxList){
			FipinqboxOD fipinqboxOD = new FipinqboxOD();
			fipinqboxOD.setN_box_id_fk(ret_box.getBox_id());
			fipinqboxOD.setBox_stat(ret_box.getBox_stat());
			fipinqboxOD.setMdl_id_fk(ret_box.getMdl_id_fk());
			fipinqboxOD.setPrd_grade_fk(ret_box.getPrd_grade_fk());
			fipinqboxOD.setPrd_qty(String.valueOf(ret_box.getPrd_qty()));
			fipinqboxOD.setWo_id_fk(ret_box.getWo_id_fk());
			dList.add(fipinqboxOD);
		}
		outTrx.setOaryD(dList);
		outTrx.setOaryC(oList);
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
	}
	private void getBoxByPallect(FipinqboxI inTrx, FipinqboxO outTrx) {
		String hql = "FROM Ret_box WHERE 1=1 ";
		List<Ret_box> boxList = new ArrayList<Ret_box>();
		if (!Util.isSpaceCheck(inTrx.getPallet_id())) {
			hql += " AND pallet_id_fk='" + inTrx.getPallet_id() + "'";
		}else{
            outTrx.setRtn_mesg("栈板号为空，请确认!");
            outTrx.setRtn_code(String.valueOf(E_FIPINQBOX_INVALID_INPUT_BOX_ID));
            return;
        }
		boxList = retBoxDao.find(hql);
		List<Ret_prd_info> ret_prd_infoList =  new ArrayList<>();
		for(Ret_box ret_box :boxList ){
			List<Ret_prd_info> ret_prd_info=retPrdInfoDao.getShtListInfo(ret_box.getBox_id());
			ret_prd_infoList.addAll(ret_prd_info);
		}
		if (ret_prd_infoList != null ){
			if (!ret_prd_infoList.isEmpty()) {
				outTrx.setOary(ret_prd_infoList);
			}
		}
//		//获取 box for prd max count
//		String hql1 = "From Bis_data where data_cate='BXSC' AND data_ext='"+boxList.get(0).getBox_set_code()+"'";
//		List<Bis_data> bisDataList = bisDataDao.find(hql1);
//		if(bisDataList.size()>0){
//            outTrx.setMax_cnt(bisDataList.get(0).getExt_1());//箱子的最大装载量
//        }

		//查询客户名称
		Bis_mdl_def  bis_mdl_def = bisMdlDefDao.get(boxList.get(0).getMdl_id_fk());
		if(bis_mdl_def != null){
			outTrx.setCus_mdl_id(bis_mdl_def.getCus_mdl_id());
			outTrx.setMdl_size(bis_mdl_def.getMdl_size());
			Bis_cus_loc  bis_cus_loc = bisCusDao.get(bis_mdl_def.getCus_id());
			if(bis_cus_loc != null){
				outTrx.setCus_name(bis_cus_loc.getCus_name());
			}else{
				outTrx.setRtn_mesg("客户代码["+bis_mdl_def.getCus_id()+"]不存在，请确认！");
				outTrx.setRtn_code(String.valueOf(E_BIS_CUS_LOC + E_READ_NOT_FOUND));
				return;
			}
        }else{
			outTrx.setRtn_mesg("产品代码["+boxList.get(0).getMdl_id_fk()+"]不存在，请确认！");
			outTrx.setRtn_code(String.valueOf(E_BIS_MDL_DEF + E_READ_NOT_FOUND));
			return;
		}

		outTrx.setOary_box(boxList);
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
	}
	
	/*public void moveInQueryFnc(FipinqboxI inTrx,FipinqboxO outTrx) {
		String tool_id = inTrx.getTool_id();
		String box_id = inTrx.getBox_id();
		String hql = "FROM Ret_box where 1=1 ";
		if (box_id != null) {
			hql = hql + " and id.box_id ='" + box_id + "'";
		}else if(tool_id != null){
			hql += "and tool_id_fk ='"+tool_id+"'";
		}
		List<Ret_box> retBoxList = retBoxDao.find(hql);

		if (retBoxList.isEmpty()) {
			outTrx.setRtn_code(E_RET_BOX + E_READ_NOT_FOUND + "");
			outTrx.setRtn_mesg("未找到箱号信息！");
		}
		List<Ret_box> oaryList = new ArrayList<Ret_box>();
		for (Ret_box retBox : retBoxList) {
			FipinqboxO oary = new FipinqboxO();
			oary.setBox_id(retBox.getBox_id());
			oary.setBox_stat(retBox.getBox_stat());
			oary.setPrd_qty(retBox.getPrd_qty());
			oary.setTool_id_fk(retBox.getTool_id_fk());
			oary.setWo_id_fk(retBox.getWo_id_fk());
			oary.setMdl_id_fk(retBox.getMdl_id_fk());
			oary.setCus_no(retBox.getCus_no());
			oaryList.add(oary);
		}
		outTrx.setOary(oaryList);
		outTrx.setTbl_cnt(oaryList.size());
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
	}*/
	private void queryForPrint(FipinqboxI inTrx, FipinqboxO outTrx) {
		FipinqboxIA iary = inTrx.getIary().get(0);
		String productId = iary.getPrductId();
		String typeA     = iary.getTypeA();
		String typeB     = iary.getTypeB();

		Ret_box ret_box = new Ret_box();
		Bis_mdl_def bis_mdl_def = new Bis_mdl_def();
		if("B".equals(typeA)){
			ret_box =retBoxDao.get(productId);
			if(ret_box==null){
				outTrx.setRtn_mesg("箱子["+productId+"]不存在，请确认！");
				outTrx.setRtn_code(String.valueOf(E_FIPINQBOX_INVALID_INPUT_BOX_ID));
				return;
			}
			bis_mdl_def = bisMdlDefDao.get(ret_box.getMdl_id_fk());
		}
		if("P".equals(typeA)){
			if("W".equals(typeB)){
				Ret_prd_info ret_prd_info = retPrdInfoDao.get(productId);
				if(ret_prd_info==null){
					outTrx.setRtn_mesg("玻璃["+productId+"]不存在，请确认！");
					outTrx.setRtn_code(String.valueOf(E_FIPINQBOX_INVALID_INPUT_BOX_ID));
					return;
				}
				if(ret_prd_info.getShip_box_id_fk()==null){
					outTrx.setRtn_mesg("玻璃["+productId+"]外箱不存在，请确认！");
					outTrx.setRtn_code(String.valueOf(E_FIPINQBOX_INVALID_INPUT_BOX_ID));
					return;
				}
				ret_box =retBoxDao.get(ret_prd_info.getShip_box_id_fk());
				if(ret_box==null){
					outTrx.setRtn_mesg("箱子["+ret_box.getBox_id()+"]不存在，请确认！");
					outTrx.setRtn_code(String.valueOf(E_FIPINQBOX_INVALID_INPUT_BOX_ID));
					return;
				}
				bis_mdl_def = bisMdlDefDao.get(ret_box.getMdl_id_fk());
			}
			if("N".equals(typeB)){
				Ret_prd_info ret_prd_info = retPrdInfoDao.get(productId);
				if(ret_prd_info==null){
					outTrx.setRtn_mesg("玻璃["+productId+"]不存在，请确认！");
					outTrx.setRtn_code(String.valueOf(E_FIPINQBOX_INVALID_INPUT_BOX_ID));
					return;
				}
				if(ret_prd_info.getBox_id_fk()==null){
					outTrx.setRtn_mesg("玻璃["+productId+"]内箱不存在，请确认！");
					outTrx.setRtn_code(String.valueOf(E_FIPINQBOX_INVALID_INPUT_BOX_ID));
					return;
				}
				ret_box =retBoxDao.get(ret_prd_info.getBox_id_fk());
				if(ret_box==null){
					outTrx.setRtn_mesg("箱子["+ret_box.getBox_id()+"]不存在，请确认！");
					outTrx.setRtn_code(String.valueOf(E_FIPINQBOX_INVALID_INPUT_BOX_ID));
					return;
				}
				bis_mdl_def = bisMdlDefDao.get(ret_box.getMdl_id_fk());
			}
		}
		       outTrx.setBox_id(ret_box.getBox_id());
               outTrx.setWo_id_fk(ret_box.getWo_id_fk());
		       outTrx.setMdl_id_fk(ret_box.getMdl_id_fk());
		       outTrx.setMdl_desc(bis_mdl_def.getMdl_dsc());
		       outTrx.setPrd_qty(ret_box.getPrd_qty());
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
	}

	private void queryForMtrlBox(FipinqboxI inTrx, FipinqboxO outTrx) {
		String mtrlBoxId = inTrx.getMtrl_box_id();
		Ret_prd_in ret_prd_in = retPrdInDao.get(mtrlBoxId);
		if(null==ret_prd_in){
			outTrx.setRtn_mesg("来料箱["+mtrlBoxId+"]不存在，请确认！");
			outTrx.setRtn_code(String.valueOf(E_FIPINQBOX_INVALID_INPUT_BOX_ID));
			return;
		}
        outTrx.setMtrl_prod_id_fk(ret_prd_in.getMtrl_prod_id_fk());
		outTrx.setPrd_act_qty(ret_prd_in.getPrd_act_qty().toString());
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
	}

}
